package top.igotcha.chapter.chapter8.countdownLatch;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Gotcha
 * @date 2021/3/19
 * @describe
 */
@Slf4j(topic = "CountdownLatchTest01")
public class CountdownLatchTest01 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(3);
        ExecutorService pool = Executors.newFixedThreadPool(4);
        pool.execute(()->{
            log.debug("线程任务开始");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            latch.countDown();
            log.debug("线程任务结束，{}",latch.getCount());
        });
        pool.execute(()->{
            log.debug("线程任务开始");
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            latch.countDown();
            log.debug("线程任务结束，{}",latch.getCount());
        });
        pool.execute(()->{
            log.debug("线程任务开始");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            latch.countDown();
            log.debug("线程任务结束，{}",latch.getCount());
        });
        latch.await();
    }
}

